package youleche.com.controler.servlet;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.interfaces.AdminUtil;

/**
 * Servlet implementation class STempButtonsServlet
 */
public class STempButtonsServlet extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public STempButtonsServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action") == null ? "0" : request.getParameter("action"));
		switch(action){
			case 0: 
				///清空租车上传图片临时库
				this.cleanTemp_Rent(request, response);
				break;
		}
	}
	
	/**
	 * 清空租车上传图片临时库
	 * @param request
	 * @param response
	 */
	private void cleanTemp_Rent(HttpServletRequest request, HttpServletResponse response){
		if(!AdminUtil.isAdmin(request, response)){
			super.response(response, "0");
			return;
		}
		
		String tempDir = this.getServletConfig().getServletContext().getRealPath("") + "/vehicleImageTemp/";
		File file=new File(tempDir);
		File file_only = null;
		String[] fileNameArr = file.list();
		for (String fileName : fileNameArr) {
			file_only= new File(tempDir +fileName);
			if(file_only.exists()){
				file_only.delete();
			}
		}
		
		super.response(response, "1");
		
	}

}
